package home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;
import net.ck.hzq.WebviewActivity;
import net.ck.hzq.view.LearnMoreActivity;

import java.util.ArrayList;
import java.util.List;

import Bean.HomeTargetBean;
import Utils.ScreenUtil;
import ui.activity.profit.ProfitAct;
import ui.activity.profit.RankingAct;
import ui.activity.profit.SignAct;
import util.ToastUtils;

public class TopTargetAdapter extends DelegateAdapter.Adapter<TopTargetAdapter.TopTargetVH> {

    private final Context mContext;
    private List<HomeTargetBean> mData;
    private String backurl;
    private String wweburl;

    public TopTargetAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setupData(@NonNull List<HomeTargetBean> data, String url, String weburl) {
        mData.clear();
        backurl = url;
        wweburl = weburl;
        mData.addAll(data);
//        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        GridLayoutHelper helper = new GridLayoutHelper(4);
        helper.setVGap(ScreenUtil.dip2px(mContext, 20));
        helper.setMargin(0, ScreenUtil.dip2px(mContext, 0),
                0, ScreenUtil.dip2px(mContext, 10));
        return helper;
    }

    @NonNull
    @Override
    public TopTargetVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new TopTargetVH(LayoutInflater.from(mContext).inflate(R.layout.item_home_toptarget, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final TopTargetVH holder, final int position) {
        if (position < 0 || position >= mData.size()) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        if (position == 0) {
            Glide.with(mContext)
                    .load(backurl)
                    .apply(options)
                    .into(holder.backimg);
        }
        HomeTargetBean targetBean1 = mData.get(0);
        Glide.with(mContext)
                .load(targetBean1.iconResId)
                .into(holder.icon1);
        holder.title1.setText(targetBean1.title);
        holder.wrapper1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                Intent intent = new Intent(mContext, SignAct.class);
                mContext.startActivity(intent);
            }
        });

        HomeTargetBean targetBean2 = mData.get(1);
        Glide.with(mContext)
                .load(targetBean2.iconResId)
                .into(holder.icon2);
        holder.title2.setText(targetBean2.title);
        holder.wrapper2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                Intent intent = new Intent(mContext, ProfitAct.class);
                mContext.startActivity(intent);
            }
        });

        HomeTargetBean targetBean3 = mData.get(2);
        Glide.with(mContext)
                .load(targetBean3.iconResId)
                .into(holder.icon3);
        holder.title3.setText(targetBean3.title);
        holder.wrapper3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                Intent intent = new Intent(mContext, RankingAct.class);
                mContext.startActivity(intent);
            }
        });

        HomeTargetBean targetBean4 = mData.get(3);
        Glide.with(mContext)
                .load(targetBean4.iconResId)
                .into(holder.icon4);
        holder.title4.setText(targetBean4.title);
        holder.wrapper4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                Intent intent = new Intent(mContext, LearnMoreActivity.class);
                mContext.startActivity(intent);
            }
        });

        holder.backimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                if (wweburl != null) {
                    WebviewActivity.start(mContext, wweburl);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class TopTargetVH extends RecyclerView.ViewHolder {
        private ImageView icon1;
        private TextView title1;
        private LinearLayout wrapper1;
        private ImageView icon2;
        private TextView title2;
        private LinearLayout wrapper2;
        private ImageView icon3;
        private TextView title3;
        private LinearLayout wrapper3;
        private ImageView icon4;
        private TextView title4;
        private LinearLayout wrapper4;
        private ImageView backimg;

        TopTargetVH(@NonNull View itemView) {
            super(itemView);
            icon1 = itemView.findViewById(R.id.top_icon1);
            title1 = itemView.findViewById(R.id.top_title1);
            icon2 = itemView.findViewById(R.id.top_icon2);
            title2 = itemView.findViewById(R.id.top_title2);
            icon3 = itemView.findViewById(R.id.top_icon3);
            title3 = itemView.findViewById(R.id.top_title3);
            icon4 = itemView.findViewById(R.id.top_icon4);
            title4 = itemView.findViewById(R.id.top_title4);
            backimg = itemView.findViewById(R.id.top_hdbackimg);
            wrapper1 = itemView.findViewById(R.id.wrapper1);
            wrapper2 = itemView.findViewById(R.id.wrapper2);
            wrapper3 = itemView.findViewById(R.id.wrapper3);
            wrapper4 = itemView.findViewById(R.id.wrapper4);
        }
    }

}
